﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Jungle.Code.Utils;
using Jungle.Code.DAL;
using Jungle.Code.ProductManager;


namespace Jungle
{
    public partial class RssCreator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            SqlConnection connString = new SqlConnection(DBHelper.ConnectionString);

            string catId = "";

            if (Request["catId"] != null)
                catId = Request["catId"];
            else
                catId = "1";

            string sUrl = UrlHelper.GetSiteUrl();            

            SqlCommand GetNewsCmd = connString.CreateCommand();
            
            GetNewsCmd.CommandText = "SELECT [Title], [ImagePath], [ProductId], [AddDate] FROM [catalog].[Products] WHERE (DATEDIFF(d,AddDate,CURRENT_TIMESTAMP) < 10 AND [CategoryId] = " + catId + " AND [Visible] = 'True') ORDER BY AddDate;";
            GetNewsCmd.CommandType = CommandType.Text;
                  
            connString.Open();
            GetNewsCmd.ExecuteNonQuery();

            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = GetNewsCmd;

            
            DataSet ds = new DataSet();
            sda.Fill(ds, "xml");
            DataTable dt = ds.Tables["xml"];


            string strRss = "";
            strRss += @"<?xml version='1.0' encoding='windows-1251' ?>
                    <rss version='2.0'>
	                    <channel>
		                        <title>Лента новостей сайта  Omofor</title>
		                        <link>http://omofor.gadukino.ua</link>
		                        <description>Последние товары в каталоге.</description>";

            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {
                    if (j == 0) { strRss += "\n<item>\n\t<title>" + dt.Rows[i][j].ToString().Trim() + "</title>"; }
                    if (j == 1) 
                    {
                        if (dt.Rows[i][j].ToString() == "No photo") {
                        strRss += "\n\t<description>&lt;img src=&quot;" + sUrl + "/UserFiles/Images/Content/no_photo.jpg&quot;&gt;</description>"; 
                    } 
                    else 
                    { 
                        strRss += "\n\t<description>&lt;img src=&quot;" + sUrl + dt.Rows[i][j].ToString() + "&quot;&gt;</description>"; 
                    }
                    }
                    if (j == 2) { strRss += "\n\t<link>" + sUrl + "/ru/main.aspx?cmd=viewProduct&amp;pId=" + dt.Rows[i][j].ToString() + "</link>"; }
                    if (j == 3) { strRss += "\n\t<pubDate>" + dt.Rows[i][j].ToString() + "</pubDate>\n</item>"; }
                }
            }


            strRss += @"</channel></rss>";
            Response.ContentType = "text/xml";
            Response.Write(strRss);
       



        }

        protected override void OnInit(EventArgs e)
        {
            
        }
    }
}